package com.google.coffeeshop.business;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;

import vn.bang.utility.DateUtil;
import vn.bang.utility.JpaUtil;

import com.google.coffeeshop.dao.DAOFactory;
import com.google.coffeeshop.entity.EmployeeDTO;
import com.google.coffeeshop.entity.ProductDTO;
import com.google.coffeeshop.entity.ProductGroupDTO;
import com.google.coffeeshop.entity.ProductSellingPriceDTO;
import com.google.coffeeshop.entity.ReceiptNoteDTO;
import com.google.coffeeshop.entity.ReceiptNoteLineDTO;
import com.google.coffeeshop.entity.SeatDTO;
import com.google.coffeeshop.log.Log;


public class ManagerBusiness extends AbstractBaseBusiness {

	static public EmployeeDTO findEmployeeWorkingById(long id) {
		EmployeeDTO employees = null;
		final EntityManager entityManager = createEntityManager();
		try {
			employees = DAOFactory.createEmployeeDAO(entityManager).findWorkingById(id);
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return employees;
	}
	static public void saveProductGroup(ProductGroupDTO productGroupDTO) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
				ProductGroupDTO productGroup = productGroupDTO;
				if (productGroup != null) {
					DAOFactory.createProductGroupDAO(entityManager).save(productGroup);
				}
			JpaUtil.commitEntityManager(entityManager);
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
	}
	
	
	static public SeatDTO findSeatById(long seatId) {
		SeatDTO seat = null; 
		final EntityManager entityManager = createEntityManager();
		try {
			seat = DAOFactory.createSeatDAO(entityManager).find(seatId);
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return seat;
	}
	static public ProductSellingPriceDTO findLatestPriceById(long productId) {
		ProductSellingPriceDTO productPrice = null;
		final EntityManager entityManager = createEntityManager();
		try {
			productPrice = DAOFactory.createProductPriceDAO(entityManager).findLastestPriceByProductId(productId);
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return productPrice;
	}
	
	static public ProductDTO getProductById(long productId) {
		ProductDTO product=null; 
		final EntityManager entityManager = createEntityManager();
		try {
			product=DAOFactory.createProductDAO(entityManager).findActiveById(productId);
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return product;
	}
	
	static public ProductGroupDTO getProductGroupById(long groupId) {
		ProductGroupDTO group=null; 
		final EntityManager entityManager = createEntityManager();
		try {
			group=DAOFactory.createProductGroupDAO((entityManager)).findProductGroupById(groupId);
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return group;
	}
	
	static public List<EmployeeDTO> findAllEmployee() {
		List<EmployeeDTO> employees = new ArrayList<EmployeeDTO>();
		final EntityManager entityManager = createEntityManager();
		try {
			employees.addAll(DAOFactory.createEmployeeDAO(entityManager).findAll());
		} catch(PersistenceException persistenceException) {
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return employees;
	}
	
	static public List<EmployeeDTO> leaveEmployee(Long employeeId) {
		List<EmployeeDTO> employees = new ArrayList<EmployeeDTO>();
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
				EmployeeDTO employee = DAOFactory.createEmployeeDAO(entityManager).find(employeeId);
				if (employee != null) {
					employee.setEndDate(DateUtil.getNow());
					DAOFactory.createEmployeeDAO(entityManager).save(employee);
				}
				
			JpaUtil.commitEntityManager(entityManager);
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return employees;
	}
	
	static public void saveSeat(SeatDTO seat) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
				SeatDTO seatDTO = seat;
				if (seatDTO != null) {
					DAOFactory.createSeatDAO(entityManager).save(seatDTO);
				}
			JpaUtil.commitEntityManager(entityManager);
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
	}
	
	static public void saveEmployee(EmployeeDTO employeeDTO) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
				EmployeeDTO employee = employeeDTO;
				if (employee != null) {
					DAOFactory.createEmployeeDAO(entityManager).save(employee);
				}
			JpaUtil.commitEntityManager(entityManager);
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
	}
	
	public static ProductDTO saveProduct(ProductDTO productDTO) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
			ProductDTO product = productDTO;
				if (product != null) {
					DAOFactory.createProductDAO(entityManager).save(product);
				}
			JpaUtil.commitEntityManager(entityManager);
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
		return productDTO;
	}
	
	public static void saveSellingPrice(ProductSellingPriceDTO sellingPriceDTO) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
			ProductSellingPriceDTO sellingPrice = sellingPriceDTO;
				if (sellingPrice != null) {
					DAOFactory.createProductPriceDAO(entityManager).save(sellingPrice);
				}
			JpaUtil.commitEntityManager(entityManager);
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
	}
	
	public static void saveReceiptNoteLine(ReceiptNoteLineDTO receiptNoteLine) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
			if (receiptNoteLine != null) {
				DAOFactory.createReceiptNoteLineDAO(entityManager).save(receiptNoteLine);
			}
			JpaUtil.commitEntityManager(entityManager);
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
	}
	
	public static void removeReceiptNote(long receiptNoteId) {
		final EntityManager entityManager = createEntityManager();
		try {
			JpaUtil.beginTransaction(entityManager);
				ReceiptNoteDTO receiptNote = DAOFactory.createReceiptNoteDAO(entityManager).find(receiptNoteId);
				DAOFactory.createReceiptNoteDAO(entityManager).save(receiptNote);
			JpaUtil.commitEntityManager(entityManager);
		} catch(PersistenceException persistenceException) {
			JpaUtil.rollbackEntityManager(entityManager);
			Log.error(persistenceException);
		} finally {
			JpaUtil.closeEntityManager(entityManager);
		}
	}
	
}
